package cc.comac.mom.datahandlers.entity;

import java.util.ArrayList;
import java.util.List;

import cc.comac.mom.util.*;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cc.comac.mom.bean.EquipMaintainWorkOrder;
import cc.comac.mom.bean.EquipMaintainWorkOrderBean;
import cc.comac.mom.bean.ResultDev;
import cc.comac.mom.dao.entity.EquipMaintainWorkOrderDao;
import cc.comac.mom.entity.EquipMaintainWorkOrderEntity;

@Component
@Transactional
public class EquipMaintainWorkOrderDataHandler
		extends ServiceImpl<EquipMaintainWorkOrderDao, EquipMaintainWorkOrderEntity> {

	@Autowired
	private BaseEntityDataUtil baseEntityDataUtil;

	@Autowired
	private UniversalNumberGenerationDataHandler universalNumberGenerationDataHandler;

	@Autowired
	private EquipMaintainWorkOrderDao equipMaintainWorkOrderDao;

	// 批量新增保养工单数据
	public ResultBean batchInsertMaintainWorkOrder(List<EquipMaintainWorkOrderEntity> list) {
		ResultBean resultBean = new ResultBean();
		if (list.size() > 0) {
			for (EquipMaintainWorkOrderEntity equipMaintainWorkOrderEntity : list) {
				String numberGeneration = universalNumberGenerationDataHandler
						.numberGeneration(universalNumberGenerationDataHandler.getMaintainWork());
				equipMaintainWorkOrderEntity.setOrderNumber(numberGeneration);
				baseEntityDataUtil.setBaseEntityData(equipMaintainWorkOrderEntity);
			}
			saveBatch(list, list.size());
		}
		return resultBean;
	}

	// 逻辑删除对应数据
	public ResultBean batchDeleteMaintainWorkOrder(List<EquipMaintainWorkOrder> list) {
		ResultBean resultBean = new ResultBean(true);
		if (list.size() > 0) {
			// 获取集合主键
			List<Long> ids = new ArrayList<Long>();
			for (EquipMaintainWorkOrder equipMaintainWorkOrder : list) {
				ids.add(equipMaintainWorkOrder.getInnerData().getId());
			}
			// 查询对应集合
			List<EquipMaintainWorkOrderEntity> selectBatchIds = equipMaintainWorkOrderDao.selectBatchIds(ids);
			for (EquipMaintainWorkOrderEntity equipMaintainWorkOrderEntity : selectBatchIds) {
				baseEntityDataUtil.updateBaseEntityData(equipMaintainWorkOrderEntity);
				equipMaintainWorkOrderEntity.setRowStatus(0);
			}
			// 逻辑删除对应数据
			this.updateBatchById(selectBatchIds, selectBatchIds.size());
		}
		return resultBean;
	}
	// 派工 调整日期，调整执行人
	public ResultBean updateBySomeField (EquipMaintainWorkOrder equipMaintainWorkOrder)  {
		ResultBean resultBean = new ResultBean(true);
		EquipMaintainWorkOrderEntity innerData = equipMaintainWorkOrder.getInnerData();
		if (innerData.getId() == 0) {
			throw new RuntimeException("无法获取当前主键");
		}
		EquipMaintainWorkOrderEntity selectById = equipMaintainWorkOrderDao.selectById(innerData.getId());
		if (selectById != null) {
			baseEntityDataUtil.updateBaseEntityData(selectById);
			if (StringUtils.isNotEmpty(innerData.getExecutionUser())) {
				selectById.setExecutionUser(innerData.getExecutionUser());
				selectById.setExecutionUserJobNumber(innerData.getExecutionUserJobNumber());
			}
			if (innerData.getPlanDateStart() != null) {
				selectById.setPlanDateStart(innerData.getPlanDateStart());
				int cycleDataToHouse2 = FrequencyUtil.getCycleDataToHouse(innerData.getExecutionDurationType(),innerData.getExecutionDuration());
				selectById.setPlanDateEnd(TimeUtil.fomatLocalDateTimeToDate(TimeUtil.fomatDateToLocalDateTime(innerData.getPlanDateStart()).plusHours(cycleDataToHouse2)));
			}
			if("0".equals(innerData.getStatus())){
				selectById.setStatus(MomConstants.EQUIP_MAINTAIN_ORDER_STATUS_NOEXECUTE);
			}
			equipMaintainWorkOrderDao.updateById(selectById);
		}
		return resultBean;
	}
	// 查询对应保养工单数据通过条件
	public ResultDev selectBySomeField(EquipMaintainWorkOrderBean obj) {
		ResultDev resultDev = new ResultDev();
		if (obj.getPageSize() == 0) {
			obj.setPageSize(10);
		}
		obj.setPageNum((obj.getPageNum()-1)*obj.getPageSize());
		List<EquipMaintainWorkOrderBean> list = equipMaintainWorkOrderDao.selectByFields(obj);
		long countByFields = equipMaintainWorkOrderDao.selectCountByFields(obj);

		// 将查询总数除以每页数量后向上取整获取总页数
		double pageCount = Math.ceil((double)countByFields / obj.getPageSize());
		resultDev.setEntityList(list);
		resultDev.setPageCount((int)pageCount);
		resultDev.setTotalQuantity((int)countByFields);
		return resultDev;
	}
	
}
